<script setup lang="ts">
// 市场-市场排名-一键转换

import {message} from "ant-design-vue";
import {getCurrentInstance} from "vue";
import BaseApp from "~/contentScripts/views/sycm/baseApp.vue";
import MarketRankModal from "~/contentScripts/views/sycm/market_monitor/market_rank/MarketRankModal.vue";
// import MarketRankModal from "~/contentScripts/views/sycm/market_monitor/market_rank/MarketRankModal.vue";
import User from "~/contentScripts/views/tao/list/components/user.vue";
import {isLogin} from "~/logic";
import SYCM from "~/utils/ali/sycm";

const loginVisible = ref<boolean>(false);
const visible = ref<boolean>(false);
const data = ref<any>([]);
const tab1 = ref<any>(null);
const tab2 = ref<any>(null);
const loading = ref(false);
const __option = getCurrentInstance()?.appContext.provides.option;
const handle = async () => {
  if (isLogin({
    emit: () => {
      loginVisible.value = true;
    },
  }, __option)) {
    loading.value = true;
    let cate = document.querySelector('.ebase-FaCommonFilter-module__left .oui-pro-common-picker .common-picker-header')?.title;
    let tab = await SYCM.getInternalFiberData('.ebase-FaCommonFilter-module__left>div>span.oui-tab-switch', 'memoizedProps.children[0]._owner.memoizedProps');
    let tabChild = await SYCM.getInternalFiberData('.oui-card-header-item-pull-left>span.oui-tab-switch', 'memoizedProps.children[0]._owner.memoizedProps');
    if (!tab) {
      message.error('获取父TAB失败,请重试');
      loading.value = false;
      return;
    }
    tab1.value = tab;
    if (!tabChild) {
      message.error('获取子TAB失败,请重试');
      loading.value = false;
      return;
    }
    tab2.value = tabChild;
    let tableData = await SYCM.getInternalFiberData('[id^=sycm-mc-mq-' + (tab.activeKey == 'shop' ? tab.activeKey : (tab.activeKey + '-' + tab.activeKey)) + '-' + tabChild.activeKey + '] > div.oui-card-content', 'memoizedProps.children._owner.memoizedProps.tableData');
    if (!tableData) {
      message.error('获取表格数据失败,请重试');
      loading.value = false;
      return;
    }
    SYCM.assignData(tableData.data);
    tableData.data.map((item) => {
      let date = tableData.params.dateRange.split('|');
      if (date[0] == date[1]) {
        date = date[0];
      } else {
        date = date[0] + '~' + date[1];
      }
      item.date = date;
      item.cate = cate;
      if (typeof item.shop !== 'undefined') {
        item.platform = item.shop.b2CShop ? '天猫' : '淘宝';
      }
      if (typeof item.cartHits !== 'undefined' && typeof item.cltHits !== 'undefined') {
        item.cardAndClt = item.cartHits_tran + item.cltHits_tran;
      }
    });
    data.value = tableData;
    visible.value = true;
    await nextTick(() => {
      loading.value = false;
    });
  }
};
</script>

<template>
  <base-app>
    <a-button-group>
      <user style="margin-right: 0"/>
      <a-button :loading="loading" type="primary" size="small" @click="handle">一键转化</a-button>
    </a-button-group>
    <user-login-modal v-model:login-visible="loginVisible"/>
    <MarketRankModal v-model:visible="visible" :data="data.data" :tab1="tab1" :tab2="tab2"/>
  </base-app>
</template>

<style scoped>

</style>
